package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"src/myaccount.go/src/login"
	"src/myaccount.go/src/render"
	"src/myaccount.go/src/viewer"
)

func main() {
	r := gin.Default()
	r.HTMLRender = render.CreateMyRender()
	r.Static("/assets", "./assets")

	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.GET("/test", func(c *gin.Context) {
		c.HTML(200, "test", gin.H{})
	})

	r.GET("/", login.Loginpage)

	r.POST("/login_submit", login.Login)

	authentication := r.Group("/")
	authentication.Use(login.AuthRequired)
	{
		authentication.GET("/pingtest", viewer.PingTest)
		authentication.GET("/logout", login.Logout)
		authentication.GET("/home", login.Home)

		authentication.GET("/createuser", viewer.CreateUser)
		authentication.POST("/createuser_data", login.CreateUser)
		authentication.POST("/changepass", login.ChangePass)
	}

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}
